using System.IO;
using Godot;
using ProtoBuf;

namespace Arcane.Source.Data
{
    public struct PlayerData
    {
        public StringName character;
        public int level;
        public int heatCredit;
        public int waveCredit;
        public int conceptCredit;

        public static PlayerData Load(Save save)
        {
            return new()
            {
                character = save.character,
                level = save.level,
                heatCredit = save.credits[0],
                waveCredit = save.credits[1],
                conceptCredit = save.credits[2],
            };
        }

        public Save GetSave() => new()
        {
            character = character,
            level = level,
            credits = [heatCredit, waveCredit, conceptCredit]
        };

        [ProtoContract]
        public struct Save
        {
            [ProtoMember(1)]
            public string character;
            [ProtoMember(2)]
            public int level;
            [ProtoMember(3)]
            public int[] credits;

            public static Save FromStream(Stream stream)
            {
                return Serializer.Deserialize<Save>(stream);
            }

            public static Save FromBytes(byte[] bytes)
            {
                using var mem = new MemoryStream(bytes);
                return FromStream(mem);
            }
        }
    }
}